package com.leoman.adsindex.controller;

import com.leoman.adsindex.entity.AdsIndex;
import com.leoman.adsindex.service.AdsIndexService;
import com.leoman.adsindex.service.impl.AdsIndexServiceImpl;
import com.leoman.common.controller.common.GenericEntityController;
import com.leoman.common.factory.DataTableFactory;
import com.leoman.entity.Configue;
import com.leoman.entity.FileBo;
import com.leoman.exception.GeneralExceptionHandler;
import com.leoman.post.entity.PostBase;
import com.leoman.post.service.PostBaseService;
import com.leoman.resource.service.StillService;
import com.leoman.sensitivewords.service.SensitiveWordsService;
import com.leoman.utils.FileUtil;
import com.leoman.utils.ImageUtil;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import com.leoman.welfare.entity.Welfare;
import com.leoman.welfare.service.WelfareService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by 蔡琦 on 2016/7/6 0006.
 */
@Controller
@RequestMapping(value = "admin/adsIndex")
public class AdsIndexController extends GenericEntityController<AdsIndex, AdsIndex, AdsIndexServiceImpl> {

    @Autowired
    private AdsIndexService adsIndexService;

    @Autowired
    private StillService stillService;

    @Autowired
    private WelfareService welfareService;

    @Autowired
    private SensitiveWordsService sensitiveWordsService;

    @Autowired
    private PostBaseService postBaseService;

    /**
     * 列表页面
     *
     * @return path
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "adsIndex/list";
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/list")
    public void list(HttpServletResponse response,
                     Integer draw,
                     Integer start,
                     Integer length) {
        try {

            int pageNum = getPageNum(start, length);

            Page<AdsIndex> page = adsIndexService.page(pageNum, length);
            for (AdsIndex adsIndex : page.getContent()) {
                if (StringUtils.isNotEmpty(adsIndex.getCover())) {
                    adsIndex.setCover(ImageUtil.clearUploadUrl(adsIndex.getCover()));
                }
            }

            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, emptyData);
        }
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit")
    public String edit(Long adsIndexId, Model model) {

        AdsIndex adsIndex = adsIndexService.queryByPK(adsIndexId);
        if (StringUtils.isNotEmpty(adsIndex.getCover())) {
            adsIndex.setCover(ImageUtil.clearUploadUrl(adsIndex.getCover()));
        }
        model.addAttribute("adsIndex", adsIndex);

        return "adsIndex/add";
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail")
    public String detail(Long adsIndexId, Model model) {

        adsIndexService.getInfo(adsIndexId, model);

        return "adsIndex/detail";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Integer save(Long id,
                        String title,
                        String subTitle,
                        Integer position,
                        Long categoryId,
                        Long seriesId,
                        @RequestParam(value = "imageFile", required = false) MultipartFile multipartFile) {

        // 过滤敏感字符
        title = sensitiveWordsService.replaceWord(title);
        subTitle = sensitiveWordsService.replaceWord(subTitle);

        AdsIndex adsIndex;
        if (id == null) {
            adsIndex = new AdsIndex();
            //adsIndex.setCreateDate(System.currentTimeMillis());
        } else {
            adsIndex = adsIndexService.queryByPK(id);
            adsIndex.setUpdateDate(System.currentTimeMillis());
        }

        try {
            if (null != multipartFile) {
                // 保存首页推荐封面
                FileBo fileBo = FileUtil.save(multipartFile, true);
                adsIndex.setCover(fileBo.getPath());
                adsIndex.setSuolue(fileBo.getSuoluePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        adsIndex.setTitle(title);
        adsIndex.setSubTitle(subTitle);
        adsIndex.setPosition(position);
        adsIndex.setCategoryId(categoryId);
        adsIndex.setWorkId(seriesId);

        adsIndexService.save(adsIndex);
        return 1;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Long adsIndexId) {

        AdsIndex adsIndex = adsIndexService.queryByPK(adsIndexId);
        adsIndexService.delete(adsIndex);

        return 1;
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteBatch(String ids) {
        Long[] arrayId = JsonUtil.json2Obj(ids, Long[].class);
        for (Long id : arrayId) {
            AdsIndex adsIndex = adsIndexService.queryByPK(id);
            adsIndexService.delete(adsIndex);
        }
        return 1;
    }

    /**
     * 根据类型查找资源列表
     *
     * @return list
     */
    @RequestMapping(value = "/loadResource")
    @ResponseBody
    public List<Map<String, Object>> loadResource(Long categoryId) {

        return stillService.findListByType(categoryId);
    }

    /**
     * 根据类型查找福利社列表
     *
     * @return list
     */
    @RequestMapping(value = "/loadWelfare")
    @ResponseBody
    public List<Welfare> loadWelfare(Integer categoryId) {

        return welfareService.findByType(categoryId);
    }

    /**
     * 根据类型查找帖子列表
     *
     * @return postBaseList
     */
    @RequestMapping(value = "/loadContent")
    @ResponseBody
    public List<PostBase> loadContent(Long categoryId) {

        return postBaseService.findListAllByCategoryId(categoryId);
    }
}
